package com.tuyan.aixiang.util;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;

import com.tuyan.aixiang.MainApplication;

import java.util.ArrayList;
import java.util.List;

public class WifiUtil {

    private WifiManager wifiManager;

    static class InstanceHolder {
        private static final WifiUtil instance = new WifiUtil();
    }

    private WifiUtil() {
        WifiManager wifiManager = (WifiManager) MainApplication.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        this.wifiManager = wifiManager;
    }

    public static WifiUtil getInstance() {
        return InstanceHolder.instance;
    }


    public List<ScanResult> getWifiList() {
        if (wifiManager.isWifiEnabled()) {

            return wifiManager.getScanResults();
        }
        return new ArrayList<>();

    }

    public void openSetting(Context context) {
        context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
    }


    public WifiInfo getCurrentWifi() {
        if(!wifiManager.isWifiEnabled()) return null;
        return wifiManager.getConnectionInfo();
    }

    /**
     * 判断定位服务是否开启
     *
     * @param
     * @return true 表示开启
     */
    public boolean isLocationEnabled(Context context) {
        int locationMode = 0;
        String locationProviders;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
            } catch (Settings.SettingNotFoundException e) {
                e.printStackTrace();
                return false;
            }
            return locationMode != Settings.Secure.LOCATION_MODE_OFF;
        } else {
            locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            return !TextUtils.isEmpty(locationProviders);
        }
    }

    public void openLocationSetting(Context context) {
        Intent intent =  new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        context.startActivity(intent);

    }
}
